package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.domain.ZzSendMsg;
import com.zzedu.dto.ZzSendMsgDTO;
import com.zzedu.mapper.ZzSendMsgMapper;
import com.zzedu.service.IZzSendMsgService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 短信发送记录Service业务层处理
 * 
 * @author Mr.Wang
 * @date 2025-03-07
 */
@Service
public class ZzSendMsgServiceImpl extends ServiceImpl<ZzSendMsgMapper,ZzSendMsg> implements IZzSendMsgService
{

    @Override
    public LambdaQueryWrapper<ZzSendMsg> buildQuery(ZzSendMsgDTO zzSendMsg) {
        LambdaQueryWrapper<ZzSendMsg> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(null!=zzSendMsg.getSmsType(), ZzSendMsg::getSmsType, zzSendMsg.getSmsType());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzSendMsg.getPhone()), ZzSendMsg::getPhone, zzSendMsg.getPhone());

        lambdaQueryWrapper.orderByDesc(ZzSendMsg::getId);
        return lambdaQueryWrapper;
    }

    @Override
    public Long getCountByPhone(String tel, LocalDateTime localDateTime, LocalDateTime now) {
        LambdaQueryWrapper<ZzSendMsg> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(ZzSendMsg::getPhone, tel);
        lambdaQueryWrapper.gt(ZzSendMsg::getGmtCreate,localDateTime);
        lambdaQueryWrapper.lt(ZzSendMsg::getGmtCreate,now);
        return count(lambdaQueryWrapper);
    }
}